<template>
  <layout-box one="我的承运" two="查询快递">
    <el-row :gutter="20">
      <el-col :span="9">
        <el-input
          placeholder="请输入内容"
          v-model="queryInfo.query"
          clearable
          @clear="getOrderList"
        >
          <el-button
            slot="append"
            icon="el-icon-search"
            @click="getOrderList"
          ></el-button>
        </el-input>
      </el-col>
    </el-row>
    <el-table :data="orderList" border stripe>
      <el-table-column type="index"></el-table-column>
      <el-table-column label="寄件人姓名" prop="fromName"></el-table-column>
      <!-- <el-table-column label="寄件人手机号" prop="fromPhone"></el-table-column> -->
      <el-table-column label="寄件人地址" prop="fromCity"></el-table-column>
      <el-table-column label="收件人姓名" prop="toName"></el-table-column>
      <!-- <el-table-column label="收件人手机号" prop="toPhone"></el-table-column> -->
      <el-table-column label="收件人地址" prop="toCity"></el-table-column>
      <!-- <el-table-column label="文件类型" prop="fileType"></el-table-column> -->
      <el-table-column label="物流信息" prop="currentArea">
        <template slot-scope="scope">
          <span v-if="scope.row.currentArea == null">等待物流更新</span>
          <span v-else>{{ scope.row.currentArea }}</span>
        </template>
      </el-table-column>
      <el-table-column label="状态" prop="status">
        <template slot-scope="scope">
          <span v-if="scope.row.status == -1">等待揽件</span>
          <span v-if="scope.row.status == 0">已揽收</span>
          <span v-if="scope.row.status == 1">运输中</span>
          <span v-if="scope.row.status == 2">已签收</span>
        </template>
      </el-table-column>
      <el-table-column label="操作">
        <template slot-scope="scope">
          <el-button
            v-if="scope.row.status == -1"
            class="el-icon-edit"
            type="primary"
            size="mini"
            @click="showBox"
          ></el-button>
          <el-button
            v-if="scope.row.status == -1"
            type="danger"
            icon="el-icon-delete"
            size="mini"
            @click="deleteManger(scope.row.flowId)"
          ></el-button>

          <el-button
            v-if="scope.row.status == -1"
            class="el-icon-plus"
            type="success"
            size="mini"
            @click="showProgressBox(scope.row.flowId)"
          ></el-button>
        </template>
      </el-table-column>
    </el-table>
    <!-- 分页区 -->
    <el-pagination
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
      :current-page="queryInfo.pagenum"
      :page-sizes="[5, 10, 20, 50]"
      :page-size="queryInfo.pagesize"
      layout="total, sizes, prev, pager, next, jumper"
      :total="total"
    ></el-pagination>

    <!-- 录入快递弹出框 -->
    <el-dialog
      title="录入快递"
      :visible.sync="addressVisible"
      width="40%"
      @close="handleClose"
    >
      <el-form
        :model="addressForm"
        :rules="addressFormRules"
        ref="addressFormRef"
        label-width="120px"
      >
        <el-form-item label="寄件人姓名" prop="fromName">
          <el-input v-model="express.fromName"></el-input>
        </el-form-item>
        <el-form-item label="寄件地址" prop="fromCity">
          <el-input v-model="express.fromCity"></el-input>
        </el-form-item>
        <el-form-item label="寄件人手机号" prop="fromPhone">
          <el-input v-model="express.fromPhone"></el-input> </el-form-item
        ><el-form-item label="收件人姓名" prop="toName">
          <el-input v-model="express.toName"></el-input>
        </el-form-item>
        <el-form-item label="收件地址" prop="toCity">
          <el-input v-model="express.toCity"></el-input>
        </el-form-item>
        <el-form-item label="收件人手机号" prop="toPhone">
          <el-input v-model="express.toPhone"></el-input>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="addressVisible = false">取 消</el-button>
        <el-button type="primary" @click="expressEntry">确 定</el-button>
      </span>
    </el-dialog>

    <!-- 物流进度对话框 -->
    <el-dialog
      title="物流进度"
      :visible.sync="showProgressVisible"
      width="50%"
      @close="handleClose"
    >
      <el-form
        :model="progressInfo"
        :rules="addressFormRules"
        ref="addressFormRef"
        label-width="100px"
      >
        <el-form-item label="寄件人" prop="fromName">
          <el-input v-model="progressInfo.fromName"></el-input>
        </el-form-item>
        <el-form-item label="寄件地址" prop="fromCity">
          <el-input v-model="progressInfo.fromCity"></el-input> </el-form-item
        ><el-form-item label="收件人" prop="context">
          <el-input v-model="progressInfo.context"></el-input>
        </el-form-item>
        <el-form-item label="收件地址" prop="context">
          <el-input v-model="progressInfo.context"></el-input>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="addressVisible = false">取 消</el-button>
        <el-button type="primary" @click="expressEntry">确 定</el-button>
      </span>
    </el-dialog>
  </layout-box>
</template>

<script>
export default {
  data() {
    return {
      queryInfo: {
        query: "",
        pagenum: 1,
        pagesize: 10,
      },
      total: 0,
      orderList: [],
      addressVisible: false,
      //   默认物流对话框是关闭状态的
      showProgressVisible: false,
      addressForm: {
        address1: [],
        address2: "",
      },
      addressFormRules: {
        address1: [
          { required: true, message: "请输入省市区/县", trigger: "blur" },
        ],
        address2: [
          {
            required: true,
            message: "请输入详情地址",
            trigger: "blur",
          },
        ],
      },
      //   物流信息
      progressInfo: [],
      express: {},
    };
  },
  created() {
    this.getOrderList();
  },
  methods: {
    // 录入快递信息
    async expressEntry() {
      console.log(this.express);
      const { data: res } = await this.$http.post("/express/add", {
        fromName: this.express.fromName,
        fromCity: this.express.fromCity,
        fromPhone: this.express.fromPhone,
        toName: this.express.toName,
        toCity: this.express.toCity,
        toPhone: this.express.toPhone,
      });
      if (res.code != 200) {
        return this.$message.error("寄件失败");
      }
      this.$message.success("寄件成功");
      this.addressVisible = false;
      console.log(res);
      this.getOrderList();
    },
    // 获取快递信息
    async getOrderList() {
      // 查询所有
      const { data: res } = await this.$http.post("/express/getAll", {
        query: this.queryInfo.query,
        pagenum: this.queryInfo.pagenum,
        pagesize: this.queryInfo.pagesize,
      });
      console.log(res);
      this.orderList = res.data.expressList;
      this.total = res.data.count;
    },
    // 删除快件
    async deleteManger(id) {
      const { data: res } = await this.$http.delete("/express/delete/" + id);
      if (res.code != 200) {
        return this.$message.error("删除快件失败");
      } else {
        this.getOrderList();
      }
    },
    handleSizeChange(newSize) {
      this.queryInfo.pagesize = newSize;
      this.getOrderList();
    },
    handleCurrentChange(newPage) {
      this.queryInfo.pagenum = newPage;
      this.getOrderList();
    },
    showBox() {
      this.addressVisible = true;
    },
    handleClose() {
      this.$refs.addressFormRef.resetFields();
    },
    //承运快递
    async showProgressBox(flowId) {
      const { data: res } = await this.$http.get("/express/carriage/" + flowId);
      if (res.code != 200) {
        return this.$message.error("承运失败");
      } else {
        this.getOrderList();
      }
    },
  },
};
</script>

<style lang='less' scoped>
</style>
